package com.yanqu.road.server.cmd.activity.hanghai;

import com.yanqu.road.entity.activity.hanghai.config.HangHaiConfig;
import com.yanqu.road.pb.activity.HangHaiActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.hanghai.HangHaiMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = GameProtocol.S_HANGHAI_ACTIVITY_BUY_GOODS, desc = "大航海购买/出售货物")
public class HangHaiBuyGoodsCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        HangHaiConfig config = HangHaiMgr.getHangHaiConfig();
        if (config == null || !HangHaiMgr.isInOperationTime(config)) {
            ret = GameErrorCode.E_HANGHAI_ACTIVITY_NO_OPEN;
            return;
        }

        // 检查分组
        ret = HangHaiMgr.checkGroup(packet.getPlayerId());
        if (ret != 0) {
            return;
        }

        HangHaiActivityProto.HangHaiBuyGoodsReqMsg reqMsg = HangHaiActivityProto.HangHaiBuyGoodsReqMsg.parseFrom(packet.getBytes());

        HangHaiActivityProto.CrossHangHaiBuyGoodsReqMsg.Builder builder = HangHaiActivityProto.CrossHangHaiBuyGoodsReqMsg.newBuilder();
        builder.setActivityId(config.getActivityId());
        builder.setUnionUid(player.getUserInfo().getUnionUid());
        builder.setReq(reqMsg.toBuilder());
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_HANGHAI_ACTIVITY_BUY_GOODS, builder, config.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            HangHaiActivityProto.HangHaiBuyGoodsRespMsg.Builder builder = HangHaiActivityProto.HangHaiBuyGoodsRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_HANGHAI_ACTIVITY_BUY_GOODS, builder);
        }
    }
}
